package game.support;

public class CochonImpl implements Cochon {
    private Support bas = null;
    private Support haut = null;

    public CochonImpl(Support support) {
        this.sePoser(support);
    }

    public void avancer() throws DernierEmplacementAtteintException {
        bas.recevoir(null);
        Emplacement base = (Emplacement) this.enbas();
        Emplacement emplacementSuivant = base.suivant();
        try {
            this.sePoser(emplacementSuivant.sommet());
            emplacementSuivant.recevoir(this);
        } catch (NullPointerException npe) {
            throw new DernierEmplacementAtteintException();
        }
    }

    public boolean estSur(Support emplacement) {
        return emplacement == this.enbas();
    }

    public Support sommet() {
        try { return haut.sommet(); }
        catch (NullPointerException npe) { return this; }
    }

    public void sePoser(Support support) {
        bas = support;
    }

    public void recevoir(Support top) {
        haut = top;
    }

    public Support enbas() {
        try {
            return bas.enbas();
        } catch (NullPointerException npe) {
            return this;
        }
    }

    public boolean chevauche(Cochon cochon) {
        return this.bas.equals(cochon);
    }
}
